package demo4;


import java.util.Arrays;

class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
    public int compareTo(Student o) {
//        //自定了它的比较方式
////        if(this.age>o.age) {
////            return 1;
////        }else if(this.age == o.age) {
////            return 0;
////        }else {
////            return -1;
////        }
////        return this.age - o.age;
//
//        if(this.name.compareTo(o.name)>0) {
//            return 1;
//        }else if(this.name.compareTo(o.name)==0) {
//            return 0;
//        }else {
//            return -1;
//        }
        return this.age - o.age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test {

    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",10);
        students[1]=new Student("lisi",40);
        students[2]=new Student("wangwu",30);

        System.out.println(Arrays.toString(students));
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }

    public static void main2(String[] args) {
        String str1="abc";
        String str2="bcd";
        System.out.println(str1.compareTo(str2));
    }

    public static void main1(String[] args) {
        Student student1=new Student("zhangsan",3);
        Student student2=new Student("lisi",4);

        /*
        java中的大于小于号针对的是基本数据类型
        引用数据类型不能这么比
        需要特定的方式来比较
        此时需要一个接口，这个接口里的方法可以帮助我们比较
         */
//        if(student1.compareTo(student2)>0){
//            System.out.println("studen1>student2");
//        }else {
//            System.out.println("studen1<=student2");
//        }
    }


}























